package com.onetime.mall.trading.controller;

import com.onetime.mall.trading.constants.ErrorMsg;
import com.onetime.mall.trading.entity.IdleItemEntity;
import com.onetime.mall.trading.service.IdleItemService;
import com.onetime.mall.trading.utils.PageUtils;
import com.onetime.mall.trading.utils.R;
import com.onetime.mall.trading.vo.ItemUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 二手商品表
 *
 * @author liyixuan
 * @email liyixuan106@163.com
 * @date 2022-03-26 19:38:29
 */
@RestController
@RequestMapping("idle")
public class IdleItemController {
    @Autowired
    private IdleItemService idleItemService;


    @PostMapping("add")
    public R addIdleItem(@RequestParam("userId")
                         @NotNull(message = "登录异常 请重新登录")
                         @NotEmpty(message = "登录异常 请重新登录") String shUserId,
                         @RequestBody IdleItemEntity idleItemEntity) {
        idleItemEntity.setUserId(Long.valueOf(shUserId));
        idleItemEntity.setIdleStatus(1);
        idleItemEntity.setReleaseTime(new Date());
        if (idleItemService.addIdleItem(idleItemEntity)) {
            return R.ok().put("data", idleItemEntity);
        }
        return R.error(ErrorMsg.SYSTEM_ERROR.getCode(), ErrorMsg.SYSTEM_ERROR.getMsg());
    }

    @GetMapping("lable")
    public R findIdleItemByLable(@RequestParam Map<String, Object> params) {
        PageUtils page = idleItemService.queryPageByCategory(params);
        return R.ok().put("page", page);
    }

    @GetMapping("find")
    public R findIdleItem(@RequestParam Map<String, Object> params) {
        System.out.println("搜索条件=" + params);
        PageUtils page = idleItemService.queryPageAll(params);
        return R.ok().put("page", page);
    }

    @GetMapping("info")
    public R getIdleItem(@RequestParam Long id) {
        ItemUserVo vo = idleItemService.getIdleItem(id);
        return R.ok().put("data", vo);
    }

    @GetMapping("all")
    public R getAllIdleItem(@RequestParam("userId")
                            @NotNull(message = "登录异常 请重新登录")
                            @NotEmpty(message = "登录异常 请重新登录") String shUserId) {
        Long userId = Long.valueOf(shUserId);
        List<IdleItemEntity> list = idleItemService.getAllIdelItem(userId);
        return R.ok().put("data", list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = idleItemService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        IdleItemEntity idleItem = idleItemService.getById(id);

        return R.ok().put("idleItem", idleItem);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody IdleItemEntity idleItem) {
        idleItemService.save(idleItem);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody IdleItemEntity idleItem) {
        idleItemService.updateById(idleItem);

        return R.ok();
    }

    /**
     * 删除
     */
   /* @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        idleItemService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }*/
    @RequestMapping("/delete")
    public R delete(@RequestParam("id") String id) {
        idleItemService.removeById(id);
        return R.ok();
    }
    /**
     * 批量更改商品状态
     *
     * @param ids
     * @return
     */
    @PostMapping("/change")
    public R change(@RequestParam("data") String ids) {
        String[] split = ids.split(",");
        List<Long> collect = Arrays.stream(split).map(Long::valueOf).collect(Collectors.toList());
        idleItemService.changeByIds(collect);

        return R.ok();
    }

    @PostMapping("update")
    public R updateIdleItem(@RequestParam("userId")
                            @NotNull(message = "登录异常 请重新登录")
                            @NotEmpty(message = "登录异常 请重新登录") String shUserId,
                            @RequestBody IdleItemEntity idleItemEntity) {
        Long userId = Long.valueOf(shUserId);
        idleItemEntity.setUserId(userId);
        if (idleItemService.updateIdleItem(idleItemEntity)) {
            return R.ok();
        }
        return R.error(ErrorMsg.SYSTEM_ERROR.getCode(), ErrorMsg.SYSTEM_ERROR.getMsg());
    }

}
